package com.hllxd.hism.star.basic.service.model.dapan;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;

/**
 * 工具类
 * @author lihaolong
 * @date 2018-11-27 14:24
 **/
@Slf4j
public class Utils {

    //把,分割的两个数"v,v"转化为数组[v,v]
    public static BigDecimal[] str2Arr(String content) {
        String[] items = content.split(",");
        if(items.length==0) {
            log.warn("content is invalid.{}",content);
            return null;
        }
        if(items.length==1) {
            return new BigDecimal[] {
                    new BigDecimal(items[0]).setScale(4, BigDecimal.ROUND_HALF_UP),
            };
        }
        return new BigDecimal[] {
                new BigDecimal(items[0]).setScale(4, BigDecimal.ROUND_HALF_UP),
                new BigDecimal(items[1]).setScale(4, BigDecimal.ROUND_HALF_UP)
        };
    }
    //把,分割的两个数"v:v"转化为数组[v,v]
    public static String[] str2Strs(String content){
        if(StringUtils.isEmpty(content)){
            log.warn("str2Strs content is null");
            return null;
        }
        String[] items = content.split(":");
        return items;
    }
    public static BigDecimal toBigDecimal(String val) {
        try {
            return new BigDecimal(val).setScale(4, BigDecimal.ROUND_HALF_UP);
        }catch (Exception e) {
            return BigDecimal.ZERO;
        }
    }

    //把,分割的两对数"k,v,k,v"转化为数组[v,v]
    public static BigDecimal[] str2Arr2(String content) {
        if(StringUtils.isEmpty(content)){
            log.error("content is invalid.{}",content);
            return new BigDecimal[] { BigDecimal.ZERO,BigDecimal.ZERO};
        }
        String[] items = content.split(",");
        try {
            if (items.length == 2) {
                return new BigDecimal[] {
                    new BigDecimal(items[1]).setScale(4, BigDecimal.ROUND_HALF_UP)
                };
            }
            if (items.length < 4) {
                return new BigDecimal[] {new BigDecimal(items[1]).setScale(4, BigDecimal.ROUND_HALF_UP), BigDecimal.ZERO};
            }
            if (items[3].contains("^")) {
                String[] amounts = items[3].split("\\^");
                return new BigDecimal[] {
                    new BigDecimal(items[1]).setScale(4, BigDecimal.ROUND_HALF_UP),
                    new BigDecimal(amounts[0]).setScale(4, BigDecimal.ROUND_HALF_UP),
                    new BigDecimal(amounts[1]).setScale(4, BigDecimal.ROUND_HALF_UP)
                };
            }
            return new BigDecimal[] {new BigDecimal(items[1]).setScale(4, BigDecimal.ROUND_HALF_UP),
                new BigDecimal(items[3]).setScale(4, BigDecimal.ROUND_HALF_UP)};
        }catch (Exception e) {
            log.error("{}, Exception: {}", content, e.toString());
            return new BigDecimal[] { BigDecimal.ZERO,BigDecimal.ZERO};
        }
    }

    public static String sumContent(String cnt1,String cnt2){
        Integer i1,i2;
        if(StringUtils.isEmpty(cnt1)) {
            i1 = 0;
        }
        else {
            i1 = Integer.valueOf(cnt1);
        }
        if(StringUtils.isEmpty(cnt2)) {
            i2 = 0;
        }
        else {
            i2 = Integer.valueOf(cnt2);
        }
        return String.valueOf(i1+i2);
    }

    public static String subContent(String cnt1,String cnt2){
        Integer i1,i2;
        if(StringUtils.isEmpty(cnt1)) {
            i1 = 0;
        }
        else {
            i1 = Integer.valueOf(cnt1);
        }
        if(StringUtils.isEmpty(cnt2)) {
            i2 = 0;
        }
        else {
            i2 = Integer.valueOf(cnt2);
        }
        return String.valueOf(i1-i2);
    }
}
